package com.ymatou.iapi.sellertrading.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.collections.MapUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellertrading.service.NoNeedPostpayCall;
import com.ymatou.iapi.trading.operate.parameter.OrderGroupVo;
import com.ymatou.iapi.trading.operate.parameter.OrderItemVo;
import com.ymatou.iapi.trading.operate.parameter.OrderVo;
import com.ymatou.iapi.trading.operate.parameter.SellerCouponVo;
import com.ymatou.iapi.trading.operate.parameter.YmatouCouponVo;
import com.ymatou.iapi.trading.operate.parameter.enums.AppTerminalSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.OrderSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.OrderTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.PriceTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.ProductRefundChannelEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.SalesTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.SellerCouponApplianceTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.TerminalSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.YmatouCouponApplianceTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.service.PlaceOrderCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtOrders;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtOrdersIMapper;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;

import sun.util.logging.resources.logging;


public class Ts_NoNeedPostpay {
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单无需补款");
	}

	@Before
	public void caseUp() {
       
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new NoNeedPostpayCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_NoNeedPostpay_001() {
		Logger.start(true, "订单无需补款操作成功");
		try {
			 Logger.comment("前置数据准备");
	         int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			List<YmtOrders> list  = new YmtOrdersIMapper().selectbySellerId(sellerId);
			int orderId = list.get(0).getIorderid();
	        Logger.comment("前置数据准备结束");
	        // 调用无需补款
			JSONObject aaJsonObject = new NoNeedPostpayCall(sellerId,sellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId));
			
			DefaultWapper database = new DefaultWapper();
			List<Map> ymtOrders = database.selectOrderByOderId(orderId);
			 // 调整金额
	        BigDecimal discount = (BigDecimal) ymtOrders.get(0).get("fDiscount");
	        // 总付款金额
	        BigDecimal totalPrice = (BigDecimal) ymtOrders.get(0).get("fTotalPrice");
	        // 定金金额
	        BigDecimal orderPrice = (BigDecimal) ymtOrders.get(0).get("fOrderPrice");
	        // 最近一次更新时间
	        String dLastUpdateTime =  (String) ymtOrders.get(0).get("dLastUpdateTime");
	        // 支付时间
	        String dPostPaidTime =  (String) ymtOrders.get(0).get("dPostPaidTime");
	        Logger.comment("校验订单数据变更开始");
	        Logger.verifyNotEquals(list.get(0).getDlastupdatetime(), dLastUpdateTime, "最后一次更新时间与上次不一致");
	        Logger.verifyNotEquals(list.get(0).getDpostpaidtime(), dPostPaidTime, "最后一次更新时间与上次不一致");
	        Logger.verifyEquals(discount, orderPrice.subtract(totalPrice), "验证fDiscount");
			Logger.verifyEquals(true, ymtOrders.get(0).get("bPaidInFull"), "订单设置为不需补款");
			Logger.verifyEquals(200,aaJsonObject.getInt("Code"), "响应报文-返回码");
			 Logger.comment("校验订单数据变更开始");
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_NoNeedPostpay_002() {
		Logger.start(true, "订单的状态为非等待补款状态，无需补款返回失败");
		try {
			Logger.comment("前置数据准备");
			List<YmtOrders> list  = new YmtOrdersIMapper().selectbyStatus(16);
			int sellerId = list.get(0).getIbuyerid();
			int orderId = list.get(0).getIorderid();
	        Logger.comment("前置数据准备结束");
			JSONObject aaJsonObject = new NoNeedPostpayCall(sellerId,sellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId));
			DefaultWapper database = new DefaultWapper();
			List<Map> ymtOrders = database.selectOrderByOderId(orderId);
			JSONObject aa=  (JSONObject) aaJsonObject.getJSONArray("Data").get(0);
			Logger.verifyEquals("当前状态不能操作无需补款",aa.getString("Reason"), "不需补款原因");
			Logger.verifyEquals(201,aaJsonObject.getInt("Code"), "响应报文-返回码");
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_NoNeedPostpay_003() {
		Logger.start(true, "订单不属于买手，无需补款失败");
		try {
			Logger.comment("前置数据准备");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			List<YmtOrders> list  = new YmtOrdersIMapper().selectbySellerId(sellerId);
			int NewsellerId = 1234565;
			int orderId = list.get(0).getIorderid();
	        Logger.comment("前置数据准备结束");
			JSONObject aaJsonObject = new NoNeedPostpayCall(NewsellerId,NewsellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId));
			DefaultWapper database = new DefaultWapper();
			List<Map> ymtOrders = database.selectOrderByOderId(orderId);
			JSONObject aa=  (JSONObject) aaJsonObject.getJSONArray("Data").get(0);
			Logger.verifyNotEquals(ymtOrders.get(0).get("iBuyerId"), NewsellerId, "订单所有者不正确");
			Logger.verifyEquals(201,aaJsonObject.getInt("Code"), "响应报文-返回码");
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
	
   
}
